package com.ruoyi.channel.controller;

import com.ruoyi.channel.domain.bo.TChannelDataConfBo;
import com.ruoyi.channel.domain.vo.TChannelDataConfVo;
import com.ruoyi.channel.domain.vo.TChannelDataStaffVo;
import com.ruoyi.channel.service.ITChannelDataConfService;
import com.ruoyi.channel.service.ITChannelDataStaffService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据渠道管理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/channel/channelDataConf")
public class TChannelDataConfController extends BaseController {

    private final ITChannelDataConfService iTChannelDataConfService;
    private final ITChannelDataStaffService itChannelDataStaffService;

    /**
     * 查询数据渠道管理列表
     */
    //@SaCheckPermission("channel:channelDataConf:list")
    @GetMapping("/list")
    public TableDataInfo<TChannelDataConfVo> list(TChannelDataConfBo bo, PageQuery pageQuery) {
        return iTChannelDataConfService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/noList")
    public R<List<TChannelDataConfVo>> noList(TChannelDataConfBo bo) {
        return R.ok(iTChannelDataConfService.queryList(bo));
    }

    /**
     * 导出数据渠道管理列表
     */
    //@SaCheckPermission("channel:channelDataConf:export")
    @Log(title = "数据渠道管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TChannelDataConfBo bo, HttpServletResponse response) {
        List<TChannelDataConfVo> list = iTChannelDataConfService.queryList(bo);
        ExcelUtil.exportExcel(list, "数据渠道管理", TChannelDataConfVo.class, response);
    }

    /**
     * 获取数据渠道管理详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("channel:channelDataConf:query")
    @GetMapping("/{id}")
    public R<TChannelDataConfVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        TChannelDataConfVo tChannelDataConfVo = iTChannelDataConfService.queryById(id);
        tChannelDataConfVo.setGlList(itChannelDataStaffService.staffVo("1", id).stream().map(TChannelDataStaffVo::getUserId).collect(Collectors.toList()));
        tChannelDataConfVo.setYwList(itChannelDataStaffService.staffVo("2", id));
        return R.ok(tChannelDataConfVo);
    }

    /**
     * 新增数据渠道管理
     */
    //@SaCheckPermission("channel:channelDataConf:add")
    @Log(title = "数据渠道管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TChannelDataConfBo bo) {
        return toAjax(iTChannelDataConfService.insertByBo(bo));
    }

    /**
     * 修改数据渠道管理
     */
    //@SaCheckPermission("channel:channelDataConf:edit")
    @Log(title = "数据渠道管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TChannelDataConfBo bo) {
        return toAjax(iTChannelDataConfService.updateByBo(bo));
    }

    @PostMapping("/isOpen")
    public R<Void> isOpen(@RequestBody TChannelDataConfBo bo) {
        return toAjax(iTChannelDataConfService.updateOpenByBo(bo));
    }

    /**
     * 删除数据渠道管理
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("channel:channelDataConf:remove")
    @Log(title = "数据渠道管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTChannelDataConfService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
